var httpUtils = require('../../utils/httpUtils.js');
var utils = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    detailData: {},
    wxOrderPrice: '',
    authHidden: true,
    userInfoHidden: true,
    nameInput: '',
    idCardInput: '',
    inviteUserId: ''
  },
  jumpWxOrderList: function() {
    wx.switchTab({
      url: '../takeOrderList/takeOrderList'
    })
  },
  applyforOrder: function() {
    var aaa = wx.getStorageSync('userInfo');
    console.log(aaa);
    if (utils.isNull(wx.getStorageSync('userInfo'))) { //拿到手机号码了..
      this.setData({
        authHidden: false
      });
    } else {
      var userInfo = wx.getStorageSync('userInfo');
      if (utils.isNull(userInfo.workerName) || utils.isNull(userInfo.idCard)) { //姓名身份证号为空,要录入下,防止买保险有问题.
        this.setData({
          userInfoHidden: false
        });
      } else { //调用我要微信接单的接口
        var _this = this;
        var url = app.globalData.interfaceUrl + '/order.do?method=xyjd';
        console.log(url);
        httpUtils.post(url, {
          'orderId': this.data.detailData.orderId,
          'inviteUserId': this.data.inviteUserId
        }, function(res) {
          wx.switchTab({
            url: '../takeOrderList/takeOrderList'
          });
          wx.showToast({
            title: '操作成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          });
        });
      }
    }
  },
  onLoad: function(option) {
    var orderId = option.orderId;
    var inviteUserId = option.inviteUserId;
    this.setData({
      inviteUserId: inviteUserId
    });
    var url = app.globalData.interfaceUrl + '/order.do?method=orderDetail';
    console.log(url);
    var _this = this;
    httpUtils.post(url, {
      'orderId': orderId,
      inviteUserId: inviteUserId
    }, function(res) {
      var detailData = res.data.data;
      console.log(detailData);
      //设置文件icon
      if (!utils.isNull(detailData) && !utils.isNull(detailData.files) && detailData.files.length > 0) {
        detailData.files.forEach(function(value, index) {
          if (value.v1.indexOf(".doc") != -1) {
            value.icon = '../../images/word-icon.png';
          }
          if (value.v1.indexOf(".pdf") != -1) {
            value.icon = '../../images/pdf-icon.png';
          }
          if (value.v1.indexOf(".xlsx") != -1) {
            value.icon = '../../images/excel-icon.png';
          }
        })
      }
      _this.setData({
        detailData: detailData,
        wxOrderPrice: option.wxOrderPrice
      });

    }, function(res) {
      console.log(res);
    });
  },
  callTel: function(e) { //给联系人打电话
    var tel = utils.ifNullToNullString(e.currentTarget.dataset.tel);
    var title = utils.ifNullToNullString(e.currentTarget.dataset.title);
    var content = utils.ifNullToNullString(e.currentTarget.dataset.content);
    wx.showModal({
      title: title,
      content: content,
      confirmText: '立即拨打',
      success: function(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: tel,
            success: function() {
              console.log("拨打电话成功！")
            },
            fail: function() {
              console.log("拨打电话失败！")
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },

  //获取用户输入的名字
  nameInput: function(e) {
    this.setData({
      nameInput: e.detail.value
    })
  },
  //获取用户输入的身份证
  idCardInput: function(e) {
    this.setData({
      idCardInput: e.detail.value
    })
  },
  cancel: function() {
    this.setData({
      userInfoHidden: true
    });
  },
  confirm: function(e) {
    console.log('这是啥');
    console.log(wx.getStorageSync('userInfo'));

    var name = this.data.nameInput;
    var idCard = this.data.idCardInput;

    if (!utils.isIdCardNo(idCard)) {
      wx.showToast({
        title: '请设置正确的身份证号码.',
        icon:'none',
        duration: 2000
      });
      return;
    }

    //吧框隐藏掉
    this.setData({
      userInfoHidden: true
    });
    var _this = this;
    var url = app.globalData.interfaceUrl + '/worker.do?method=updateWorkerInfo';
    console.log(url);
    httpUtils.post(url, {
      'workerName': name,
      'idCard': idCard
    }, function(res) {
      var resp = res.data;
      var userInfo = wx.getStorageSync('userInfo');
      userInfo.workerName = name;
      userInfo.idCard = idCard;
      wx.setStorageSync('userInfo', userInfo);
      console.log(JSON.stringify(userInfo));
      wx.showToast({
        title: '操作成功',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    });
  }, //获取用户手机号码
  getPhoneNumber: function(e) {
    var _this = this;
    console.log(e);
    var params = {};
    params.encryptedData = e.detail.encryptedData;
    params.iv = e.detail.iv;
    params.loginSessionId = wx.getStorageSync('loginSessionId');
    httpUtils.post(app.globalData.interfaceUrl + '/sysUser/auth.do?method=getWorkerInfo', params, function(res) {
      console.log('信了你的邪')
      console.log(res);
      wx.setStorageSync('userInfo', res.data.data);
      _this.setData({
        authHidden: true
      });
      console.log(res);
    });
  },
  previewFile: function(e) {
    var url = e.currentTarget.dataset.url;
    console.log(url);
    utils.previewFile(url);
  },
  previewPic: function(e) {
    var url = e.currentTarget.dataset.url;
    var urls = e.currentTarget.dataset.urls;
    if (!utils.isNull(urls)) {
      urls = urls.map(function(value) {
        return value.originFileUrl;
      });
      utils.previewImg(url, urls);
    }
  }
})